function WebDavNameSpaceResolver(prefix) {
  var ns = {
    'xhtml' : 'http://www.w3.org/1999/xhtml',
    'mathml': 'http://www.w3.org/1998/Math/MathML',
    'D' : 'DAV:'
  };
  return ns[prefix] || null;
}

function WebDav(uriPath, filename, dvSetting){
  if(typeof uriPath != "string" || uriPath == "")
    return;

  if(uriPath == "/")
    this.davPaths = new Array();
  else
    this.davPaths = uriPath.split("/");
  this.filename = filename;
  var_dump("[webdav.js::WebDav] davPaths: " , this.davPaths, 4);
  var_dump("[webdav.js::WebDav] filename: " , this.filename, 4);
  var_dump("[webdav.js::WebDav] dvSetting: " , dvSetting, 4);
  this.server = dvSetting.url;
  this.user = dvSetting.username;
  this.pass = dvSetting.password;
}

WebDav.prototype.save = function(content){
  // create host collection on webdav server
  var davUri = this.server;
  for(var i=0; i < this.davPaths.length; i++){
  	davUri = davUri + this.davPaths[i] + "/";
  	var res = this.mkcol(davUri);
  	if(!res) return false;
  }
  davUri = davUri + this.filename;
  var req = new customRequest('PUT', davUri, content);
  var http = new HTTPCore();
	if(typeof this.user == "string" && this.user != "")
	  http.auth(this.user, this.pass);
	var res = http.doOperation(req);
	return res;
}

WebDav.prototype.mkdir = function(){
  var davUri = this.server;
  if(typeof(this.davPaths) != "undefined" && this.davPaths != null)
    for(var i=0; i < this.davPaths.length; i++){
    	davUri = davUri + this.davPaths[i] + "/";
    	var res = this.mkcol(davUri);
    	if(!res) return false;
    }
}

WebDav.prototype.list = function(){
  var davUri = this.server + this.uriHost + "/" + MD5(this.uriPath) + "/";
  var req = new customRequest('PROPFIND', davUri, '');
  req.addRequestHeader("Depth", "1");
  var http = new HTTPCore();
	if(typeof this.user == "string" && this.user != "")
	  http.auth(this.user, this.pass);
	var res = http.doOperation(req);
  if(!res) return null;
  var result = new Array();
	debug("[webdav.js:propfind] Response Header:" + http.status, 4);
	debug("[webdav.js:propfind] Response Body:" + http.response, 4);
	var xmlDoc = http.responseXML;
	var hrefIterator = xmlDoc.evaluate( '/D:multistatus/D:response/D:href', xmlDoc, WebDavNameSpaceResolver, XPathResult.ANY_TYPE, null );
	var thisHref = hrefIterator.iterateNext();
	while (thisHref) {
	  var url = thisHref.textContent;
    eval("var webdav_regex = /" + CACHE_FILENAME_PREFIX + "([0-9]+)$/;");
    var matches = url.match(webdav_regex);
    if(matches){
      var t = 0;
      try { t = parseInt(matches[1]) } catch(ex) {}
      result.push(t);
    }
    thisHref = hrefIterator.iterateNext();
  }
  return result;
}

WebDav.prototype.read = function(ts){
  var davUri = this.server + this.uriHost + "/" + MD5(this.uriPath) + "/" + CACHE_FILENAME_PREFIX + "" + ts;
  var req = new customRequest('GET', davUri, '');
  var http = new HTTPCore();
	if(typeof this.user == "string" && this.user != "")
	  http.auth(this.user, this.pass);
	var res = http.doOperation(req);
	return http.response;
}

WebDav.prototype.mkcol = function(uri){
  var req = new customRequest('PROPFIND', uri, '');
	var http = new HTTPCore();
	if(typeof this.user == "string" && this.user != "")
	  http.auth(this.user, this.pass);
	var res = http.doOperation(req);
	debug("[webdav.js:propfind] Request URL:" + uri, 4);
	debug("[webdav.js:propfind] Result:" + res, 4);
	debug("[webdav.js:propfind] Response Header:" + http.status, 4);
	debug("[webdav.js:propfind] Response Body:" + http.response, 4);
	// create a collection
	if(http.status == "404"){
    var req = new customRequest('MKCOL', uri, '');
    var res = http.doOperation(req);
    if(http.status == "201")
      return true;
    else{
      debug("[webdav.js:MKCOL] Request URL:" + davUri, 1);
    	debug("[webdav.js:MKCOL] Response Header:" + http.status, 1);
    	debug("[webdav.js:MKCOL] Response Body:" + http.response, 1);
    	return false;
    }
	}
	return true;
}

function webdavTest(){
  var dav = new WebDav("http://www.webtoolkit.info/javascript-md5.html");
  dav.save("my web page");
  var l = dav.list();
  for(var i=0;i<l.length;i++){
    debug(new Date(l[i]),1);
    var content = dav.read(l[i]);
    debug(content,1);
  }
}